package com.assessment.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.stereotype.Service;

import com.assessment.dao.StaffMapper;
import com.assessment.entity.Staff;

@Service
public class StaffService {
	
	@Resource
	private StaffMapper staffMapper;

	public List<Staff> findAllStaff(String interviewer, String staffName) {
		// TODO Auto-generated method stub
		Staff staff=new Staff();
		staff.setInterviewer(interviewer);
		staff.setStaffName(staffName);
		return staffMapper.findAllStaff(staff);
	}
	
	public List<String> findAllInterviewer(){
		return staffMapper.findAllInterviewer();
	}
	
	//根据实习生的具体信息
	public Staff findOneStaffById(int staff_id){
		return staffMapper.findOneStaffById(staff_id);
	}
	//修改实习生的信息
	public void updateStaff(Staff staff){
		staffMapper.updateStaff(staff);
	}
	//�޸�Ա����̭״̬Ϊ2
	public void updState(Integer id){
		staffMapper.updState(id);
		}
	//查询实习生的学校分布
	public Map<String, Integer> findAllSchool(){
		List<String> schools=staffMapper.totalSchool();
		Map<String,Integer> school=new HashMap<String, Integer>();
		for (String string : schools) {
			int total=staffMapper.totalSchoolBySchool(string);
			school.put(string, total);
		}
		return school;	
	}
	//查看实习生的个数
	public int totalstaff(){
		return staffMapper.totalStaff();
	}
	//查看211的个�?
	public int totalTOO(){
		return staffMapper.totalTOO();
	}
	//根据实习生姓名查找实习生编号
	public int findStaffIdByStaffName(String staffName){
		return staffMapper.findStaffIdByStaffName(staffName);
	}
	
	//查询专业的分�?
	public Map<String, Integer> findAllProfessional(){
		List<String> professionals=staffMapper.totalProfessional();
		Map<String, Integer> professional=new HashMap<String, Integer>();
		for (String string : professionals) {
			int total=staffMapper.totalProfessionalByProfessional(string);
			professional.put(string, total);
		}
		return professional;
	}
	
	//查看男女生占比（男生�?
	public int totalStaffSexIsMan(){
		return staffMapper.totalStaffSexIsMan();
	}
	//查看学历分布
	public Map<String, Integer> findAllDegree(){
		List<String> degrees=staffMapper.totalDegree();
		Map<String, Integer> degree=new HashMap<String, Integer>();
		for (String string : degrees) {
			int total=staffMapper.totalDegreeByDegree(string);
			degree.put(string, total);
		}
		return degree;
	}
	//查看淘汰实习生的个数
	public int totalStaffStateIsT(){
		return staffMapper.totalStaffStateIsT();
	}
	
	//查看部门接纳人数占比
	public Map<String, Integer> totalStaffByDepartment(){
		Map<String, Integer> staff_dept=new HashMap<String, Integer>();
		staff_dept.put("通信政法产品事业部", staffMapper.totalStaffByDepartment("通信政法产品事业部"));
		staff_dept.put("电子政务产品事业部", staffMapper.totalStaffByDepartment("电子政务产品事业部"));
		staff_dept.put("低碳产品事业部", staffMapper.totalStaffByDepartment("低碳产品事业部"));
		staff_dept.put("税务产品事业部", staffMapper.totalStaffByDepartment("税务产品事业部"));
		staff_dept.put("实施与运维部", staffMapper.totalStaffByDepartment("实施与运维部"));
		return staff_dept;
	}
	//添加实习生
	public void insertStaff(Staff staff){
		staffMapper.insertStaff(staff);
	}
	
	//导入实习生数据
	public void importStaff(String path) throws IOException{
		List<Staff> staffs=new ArrayList<Staff>();
	  	
        FileInputStream inputStream = new FileInputStream(new File(path));  
        //读取工作簿  
        HSSFWorkbook workBook = new HSSFWorkbook(inputStream);  
        //读取工作表  
        HSSFSheet sheet = workBook.getSheetAt(0);  
        for (int i = 1; i < sheet.getLastRowNum()+1; i++) {
        	Staff staff=new Staff();
        	 HSSFRow row = sheet.getRow(i);  
			staff.setStaffName(row.getCell(0).getStringCellValue());
			if ("正常".equals(row.getCell(1).getStringCellValue())) {
				staff.setStaffState(1);
			}else{
				staff.setStaffState(2);
			}
			
			staff.setDirection(row.getCell(2).getStringCellValue());
			staff.setInterviewer(row.getCell(3).getStringCellValue());
			staff.setInterDept(row.getCell(4).getStringCellValue());
			if("男".equals(row.getCell(5).getStringCellValue())){
				staff.setStaffSex(1);
			}else{
				staff.setStaffSex(2);
			}				
			staff.setEmial(row.getCell(6).getStringCellValue());
			staff.setBirthdate(row.getCell(7).getDateCellValue());
			staff.setNativteplace(row.getCell(8).getStringCellValue());
			staff.setDegree(row.getCell(9).getStringCellValue());
			staff.setProfessional(row.getCell(10).getStringCellValue());
			staff.setSchool(row.getCell(11).getStringCellValue());
			staff.setGraduate(row.getCell(12).getDateCellValue());
			row.getCell(13).setCellType(CellType.STRING);
			staff.setTel(row.getCell(13).getStringCellValue());
			staff.setEntry(row.getCell(14).getDateCellValue());
			row.getCell(15).setCellType(CellType.STRING);
			staff.setStaffId(Integer.parseInt(row.getCell(15).getStringCellValue()));
			staffs.add(staff);
		} 
        inputStream.close();  
        workBook.close();//最后记得关闭工作簿  
        for (Staff staff2 : staffs) {
        	System.out.println(staff2.getStaffName());
			staffMapper.insertStaff(staff2);
		}
	}
	
	
	
}
